package com.ztjy.sys.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.annonation.MyLog;
import com.ztjy.bean.*;
import com.ztjy.sys.biz.JobBiz;
import com.ztjy.sys.biz.ModuleBiz;
import com.ztjy.sys.biz.PermissionBiz;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/sys/position")
public class JobController {
    @Autowired
    private JobBiz jobBiz;
    @Autowired
    private ModuleBiz moduleBiz;
    @Autowired
    private PermissionBiz permissionBiz;

    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 27);
    }

    @MyLog("部门查询的职务操作")
    @RequestMapping("/positionList")
    public String positionList(Model model, Job job, @RequestParam(value = "pageno", defaultValue = "1") Integer pageNo) {
        Integer pageSize = 6;
        PageInfo<Job> allPage = jobBiz.findAllJobsPage(job, pageNo, pageSize);
        model.addAttribute("jpInfo", allPage);
        model.addAttribute("job", job);
        return "/sys/position/positionList";
    }

    //执行添加操作
    @RequestMapping("/addJob")
    public String addJob(Job job){
        boolean flag = jobBiz.addJob(job);
        if(flag){
            return "redirect:/sys/position/positionList";
        }else {
            return "forward:/sys/position/positionAdd.html";
        }
    }

    //进入修改页面
    @RequestMapping("/goUpdate/{jobId}")
    public String goUpdateJob(@PathVariable("jobId")Integer jobId,Model model){
        Job job = jobBiz.findJobById(jobId);
        model.addAttribute("job",job);
        return  "forward:/sys/position/positionUpdate.html";
    }

    //执行修改操作
    @RequestMapping("/doUpdate")
    public String doUpdateJob(Job job){
        boolean flag = jobBiz.updateJob(job);
        if(flag){
            return "redirect:/sys/position/positionList";
        }else {
            return  "forward:/sys/position/positionUpdate.html";
        }
    }

    /**
     * 进入授权页面
     *
     * @return
     */
    @RequestMapping("/grantPage/{jobId}")
    public String toGrantPage(Model model, HttpSession session, @PathVariable("jobId") Integer jobId) {
        //左侧边栏 状态是0 所有
        List<Module> modules = moduleBiz.selectAllModule("", 0);
        //显示所有父模块 状态是0 所有
        List<Module> allParentModule = moduleBiz.findAllParentModule(0);
        model.addAttribute("modules", modules);
        model.addAttribute("pmodules", allParentModule);
        model.addAttribute("jobId", jobId);
        return "/sys/position/positionGrant";
    }

    /**
     * 授权操作
     * @param jobId
     * @param mids
     * @return
     */
    @RequestMapping("/positionGrant")
    public String positionGrant(@Param("jobId") Integer jobId, @Param("mids") String mids) {
        List<Permission> permissions = new ArrayList<Permission>();
        String[] midArr = mids.split(",");
        for (int i = 0; i < midArr.length; i++) {
            Permission perm = new Permission();
            perm.setComrid(jobId);
            perm.setCompid(Integer.parseInt(midArr[i]));
            perm.setComstate(1);
            permissions.add(perm);
        }
        Boolean flag = permissionBiz.grantPermission(jobId, permissions);
        return "redirect:/sys/position/positionList";
    }

    //选中一级菜单
    @RequestMapping("/ajaxGrant1/{jobId}")
    @ResponseBody
    public List<Module> initGrantPage1(@PathVariable("jobId") Integer jobId) {
        //状态设置为0  1级模块
        List<Module> pModule = permissionBiz.findMoudleByJob(jobId, 0, 1);
        return pModule;
    }

    //选中二级菜单
    @RequestMapping("/ajaxGrant2/{jobId}")
    @ResponseBody
    public List<Module> initGrantPage2(@PathVariable("jobId") Integer jobId) {
        //状态设置为0  2级模块
        List<Module> cModule = permissionBiz.findMoudleByJob(jobId, 0, 2);
        return cModule;
    }


    /**
     * 修改状态
     */
    @RequestMapping("/updatestate")
    public String updateState(Integer id, String chagestate){
        Job job = jobBiz.findJobById(id);
        job.setJobState(chagestate);
        jobBiz.updateJob(job);
        return "redirect:/sys/position/positionList";
    }


}
